import Vue from 'vue';
import Vuex from 'vuex';

import user from './modules/user'
import getters from './getters'

import {getConfig} from "@/api";
import { getCache, setCache } from "../utils/cache";
import { setLang } from "../utils/language";
import { Locale } from "vant";
import zhCN from 'vant/lib/locale/lang/zh-CN';
import enUS from 'vant/lib/locale/lang/en-US';
import zhHK from 'vant/lib/locale/lang/zh-HK';

Vue.use(Vuex);

const state = {
  // doMainName: 'http://192.168.50.7/',
  doMainName: process.env.NODE_ENV === 'production' ? 'http://www.zerolimit.vip/' : 'http://192.168.50.7/',
  config: {
    config: {},
    user_level_type: {},
    currency_type: {},
    finance_type: {},
  },
  withdrawAddress: null,
  cacheData: getCache(),
  selectedAddress: null,
  languageActions: false
}

const mutations = {
  SET_CONFIG: (state, config) => {
    state.config = config;
  },
  setWithdrawAddress: (state, item) => {
    state.withdrawAddress = item;
  },
  setCache: (state, count) => {
    state.cacheData = count ===0 ? 0 : (state.cacheData + Math.random() * 0.2).toFixed(2);
    setCache(state.cacheData);
  },
  setSelectAddress: (state, address) => {
    state.selectedAddress = address;
  },
  toggleLangActions: (state, status) => {
    state.languageActions = status;
  }
}

const actions = {
// 获取网站配置项
  GetConfig({commit, state, dispatch}) {
    return new Promise((resolve, reject) => {
      getConfig().then(response => {
        const config = response.data
        commit('SET_CONFIG', config);
        resolve(config)
      }).catch(error => {
        console.log("error=>获取配置信息失败")
        reject(error)
      })
    })
  },
  setLanguage: ({commit, state}, lang) => {
    if(lang === 'en') {
      Locale.use('en-US', enUS);
    } else if(lang === 'zh') {
      Locale.use('zh-CN', zhCN);
    } else {
      Locale.use('zh-HK', zhHK);
    }
    setLang(lang);
  }
}

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters,
  modules: {
    user
  }
})
